package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.constant.FriendCode;
import com.example.demo.pojo.Friend;
import com.example.demo.pojo.Player;
import com.example.demo.pojo.R;
import com.example.demo.service.FriendService;
import com.example.demo.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private PlayerService playerService;


    @GetMapping("/getMyFriend")
    public R getMyFriend(@RequestParam("name") String name){
        QueryWrapper<Friend> qw=new QueryWrapper();
        qw.lambda().eq(Friend::getAgree, FriendCode.ACCEPT.getCode());
        //驼峰命名居然失效了，不知道为什么
        qw.and(o->o.eq("my_name",name).or().eq("friend_name",name));
        List<Friend> friends = friendService.getBaseMapper().selectList(qw);
        List<String> friendName=new ArrayList<String>();
        for (Friend friend : friends) {
            if (friend.getMyName().equals(name)){
                friendName.add(friend.getFriendName());

            }
            else
                friendName.add(friend.getMyName());
        }
        QueryWrapper<Player> qw1=new QueryWrapper();
        if(friendName.size()>0){
            qw1.in("name",friendName);
            List<Player> players = playerService.getBaseMapper().selectList(qw1);
            return R.success(players);
        }
        else
            return R.success(null);
    }

    @GetMapping("/apply")
    public R apply(@RequestParam("myName") String myName,@RequestParam("friendName")String friendName){
        QueryWrapper<Friend> qw=new QueryWrapper<>();
        qw.and(o->o.eq("my_name",myName).eq("friend_name",friendName));
        QueryWrapper<Friend> qw1=new QueryWrapper();
        qw1.and(o->o.eq("my_name",friendName).eq("friend_name",myName));

        Friend friend = friendService.getBaseMapper().selectOne(qw);
        Friend friend1=friendService.getBaseMapper().selectOne(qw1);

        System.out.println(friend);
        System.out.println(friend1);
        if (friend==null && friend1==null){
            friendService.getBaseMapper().insert(new Friend(myName,friendName,0));
            return R.success("申请成功");
        }
        else if(friend!=null && friend1==null) {
            if (friend.getAgree()==1){
                return R.success("你们已经是好友了");
            }
            if (friend.getAgree()==2){
                friend.setAgree(0);
                friendService.update(friend,qw);
                return R.success("他已经拒绝过你的好友请求，已再次发送");
            }
            if (friend.getAgree()==0){
                return R.success("已经发送过好友请求");
            }
        }
        else if(friend==null && friend1!=null) {
            if (friend1.getAgree()==1){
                return R.success("你们已经是好友了");
            }
            if (friend1.getAgree()==2){
                friend1.setAgree(1);
                friendService.update(friend1,qw1);
                return R.success("你们已经是好友了");
            }
            if (friend1.getAgree()==0){
                friend1.setAgree(1);
                friendService.update(friend1,qw1);
                return R.success("已经成为好友");
            }
        }

        return R.error("未知异常",1000);

    }

    @GetMapping("/mayAccept")
    public R mayAccept(@RequestParam("myName") String myName){
        QueryWrapper<Friend> qw=new QueryWrapper<>();
        qw.lambda().eq(Friend::getFriendName,myName);
        qw.lambda().eq(Friend::getAgree,0);
        List<Friend> friends = friendService.getBaseMapper().selectList(qw);
        List names=new ArrayList();
        for (Friend friend : friends) {
            names.add(friend.getMyName());
        }
        QueryWrapper<Player> qw1=new QueryWrapper();
        if (names.size()>0){
            qw1.in("name",names);
            List<Player> players = playerService.getBaseMapper().selectList(qw1);
            return R.success(players);
        }
        else
            return R.success(null);

    }

    //回应好友申请，1接受，2拒绝
    @PostMapping("/accept")
    public R accept(@RequestParam("myName")String myName,@RequestParam("friendName") String friendsName,@RequestParam("i") Integer i){
        UpdateWrapper<Friend> uw=new UpdateWrapper<>();
        uw.lambda().eq(Friend::getMyName,myName).eq(Friend::getFriendName,friendsName).set(Friend::getAgree,i);
        boolean b = friendService.update(uw);
        return R.success(b);
    }

    //
    @GetMapping("/refuse")
    public R refuse(@RequestParam("myName")String myName){
        System.out.println(myName);
        QueryWrapper<Friend> qw=new QueryWrapper<>();
        qw.lambda().eq(Friend::getMyName,myName).eq(Friend::getAgree,2);
        List<Friend> friends = friendService.getBaseMapper().selectList(qw);
        return R.success(friends);
    }

    @PostMapping("/deleteRefuse")
    public R refuse(@RequestParam("myName")String myName,@RequestParam("friendName") String friendName){
        QueryWrapper<Friend> qw=new QueryWrapper<>();
        qw.lambda().eq(Friend::getMyName,myName).eq(Friend::getFriendName,friendName);
        if (friendService.getBaseMapper().selectOne(qw).getAgree()==1){
            return R.success("你们已经是好友了");
        }
        int delete = friendService.getBaseMapper().delete(qw);
        return R.success(delete);
    }
}
